#encoding: utf-8
class Search::DiscoverySearchController < ApplicationController
  def discovery_search
    my_limit = 6
    my_conditions  = String.new
    my_values      = Array.new
    my_conditions << "deleted=?"
    my_values.push(0)

    @order = params[:fields_order]

    if @order.blank?
      @order = ""
    end

    @page_number = params[:page_number].to_s.to_i(10)

    @tipology = params[:tipology]
    @tipology_name
    @culture = params[:culture]
    @culture_name
    @period = params[:period]
    @period_name
    @cultural_institution = params[:cultural_institution]
    @cultural_institution_name
    

    if @tipology.blank?
      @active_tipology = true      
    else
      @active_tipology = false
      @tipology_name = (CulturalHeritage::Tipology.find(:all,:conditions=>{:deleted=>[0], :id=>@tipology.to_s.to_i(10)})).at(0).name
      my_conditions << " AND tipology = ?"
      my_values.push(@tipology)
      if ! (@order.include?("Tipología"))
        @order << "Tipología, " << @tipology_name << ", "
      end
    end

    if @culture.blank?
      @active_culture = true      
    else
      @active_culture = false
      @culture_name = (CulturalHeritage::Culture.find(:all,:conditions=>{:deleted=>[0], :id=>@culture.to_s.to_i(10)})).at(0).name
      my_conditions << " AND culture = ?"
      my_values.push(@culture)
      if ! (@order.include?("Cultura"))
        @order << "Cultura, " << @culture_name << ", "
      end
    end

    if @period.blank?
      @active_period = true      
    else
      @active_period = false
      @period_name = (CulturalHeritage::Period.find(:all,:conditions=>{:deleted=>[0], :id=>@period.to_s.to_i(10)})).at(0).name
      my_conditions << " AND period = ?"
      my_values.push(@period)
      if ! (@order.include?("Periodo"))
        @order << "Periodo, " << @period_name << ", "
      end
    end

    if @cultural_institution.blank?
      @active_cultural_institution = true
    else
      @active_cultural_institution = false
      @cultural_institution_name = (CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:deleted=>[0], :id=>@cultural_institution.to_s.to_i(10)})).at(0).name
      my_conditions << " AND culturalInstitution = ?"
      my_values.push(@cultural_institution)
      if ! (@order.include?("Institución cultural"))
        @order << "Institución cultural, " << @cultural_institution_name << ", "
      end
    end

    my_values.insert(0, my_conditions)

    if @page_number.blank?
      @page_number = 1
      my_offset = 0
    else
      if @page_number == 0
        @page_number = 1
      end
      my_offset = (@page_number-1) * 6
    end

    @total_registers = CulturalHeritage::CulturalHeritage.find(:all, :conditions=> my_values).count
    pages_count = (@total_registers / my_limit) + 1

    @pages = Array.new

    i = 1
    while i <= pages_count do
      @pages.push(i.to_s)
      i = i +1
    end

    @cultural_heritage_cultural_heritages = CulturalHeritage::CulturalHeritage.find(:all, :conditions=> my_values, :limit => my_limit, :offset => my_offset)

    @json = "["

    @cultural_heritage_cultural_heritages.each do |heritage|      

      if (heritage.gmaps == true) or (heritage.obj_culturalInstitution.gmaps == true)        
        if (heritage.gmaps == true)
          if !(@json.eql?("["))
            @json = @json  << ", "
          end
          @json = @json << '{"description":"'+define_info_window_for_search(heritage)+'","title":'+'"'+heritage.name+'","sidebar":"'+heritage.name+'","lat":"'+heritage.latitude.to_s+'","lng":"'+heritage.longitude.to_s+'","id":"'+heritage.name+'"}'
        else
          if (heritage.obj_culturalInstitution.gmaps == true)
            if !(@json.eql?("["))
              @json = @json  << ", "
            end
            @json = @json << '{"description":"'+define_info_window_for_search(heritage)+'","title":'+'"'+heritage.name+'","sidebar":"'+heritage.name+'","lat":"'+heritage.obj_culturalInstitution.latitude.to_s+'","lng":"'+ (heritage.obj_culturalInstitution.longitude + i).to_s+'"}'
          end
        end        
      end
    end

    @json = @json << "]"
  end

  def define_info_window_for_search(parameter)
    if (parameter.list_avatars.length == 0) or (parameter.avatar_file_name.blank?)
      image = root_url << "images/pukara_logo.png"
    else
      if ! parameter.avatar_file_name.blank?
        image = parameter.avatar.url
      else
        image = parameter.list_avatars.at(0).avatar.url
      end
    end
    if parameter.group == 1
      referencia = "<b><a href = '/cultural_heritage/cultural_heritages/"
      institucion = "<b><a href = '/cultural_heritage/cultural_institutions/"+parameter.obj_culturalInstitution.id.to_s+"'>"+parameter.obj_culturalInstitution.name.to_s+"</a></b>"
      mensaje = "<br /><br />Patrimonio ubicado en: " + institucion+ "<br />"
    else
      referencia = "<b><a href = '/cultural_heritage/cultural_heritage_properties/"
      mensaje = ""
    end
    return referencia+parameter.id.to_s+"'>"+parameter.name.to_s+"</a>"+mensaje+"</b>"+ "<br/><br/>"+parameter.description.to_s+"<br/><br/> <img  src="+image+"  style='width: 100px; height: 100px'>"
  end

  def info_heritage
    
  end

  def gmaps4rails_sidebar
    "<span class='foo'>#{name}</span>" #put whatever you want here
  end

end
